<template>
  <el-container id="login">
    <el-main>
      <el-row type="flex" justify="center" align="middle" style="height: 100%">
        <ResetPassword v-if="isShowResetPwd" @showlogin="showResetPwd"></ResetPassword>
        <el-col v-else :span="6" class="login-form">
          <h1>四川省智能水务管理系统</h1>
          <el-form :model="userForm" :rules="ruleForm" ref="loginForm">
            <el-form-item prop="userName">
              <el-input
                v-model="userForm.userName"
                prefix-icon="el-icon-user-solid"
                placeholder="请输入手机号码"
              ></el-input>
            </el-form-item>
            <el-row>
              <el-col :span="17">
                <el-form-item prop="passWord">
                  <el-input v-model="userForm.passWord" prefix-icon="el-icon-lock" placeholder="请输入密码"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item prop="passWord">
                  <el-button type="promary" plain @click="showResetPwd">找回密码</el-button>
                </el-form-item>
              </el-col>
            </el-row>

            <el-form-item>
              <el-button type="success" style="width: 100%" @click="userLogin('loginForm')">Log In</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </el-main>
  </el-container>
</template>

<script>
import { mapMutations } from 'vuex'
import ResetPassword from './ResetPassword.vue'
import { login } from '@/config/api.js'
export default {
  name: 'DEMOIndex',
  data() {
    return {
      isShowResetPwd: false,
      userForm: {
        userName: '1',
        passWord: '1'
      },
      ruleForm: {
        userName: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 1, max: 20, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        passWord: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    ResetPassword
  },
  methods: {
    ...mapMutations(['tokenMt', 'userInfoMt']),
    userLogin(formName) {
      //  this.$refs.loginForm 获取el-form组件。
      this.$refs[formName].validate(valid => {
        // valid验证的结构，true,表单所有成员验证通过，反之，验证失败
        if (valid) {
          login({ phone: this.userForm.userName, password: this.userForm.passWord })
            .then(res => {
              console.log(res)
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: '登录成功'
                })

                this.tokenMt(res.data.Authorization)
                this.userInfoMt(res.data.admin)
                this.$router.push('/home')
              } else {
                this.$message({
                  type: 'error',
                  message: '登录失败'
                })
              }
            })
            .catch(() => {
              this.$message({
                type: 'error',
                message: '调用接口失败'
              })
            })
        } else {
          return false
        }
      })
    },
    showResetPwd() {
      this.isShowResetPwd = !this.isShowResetPwd
    }
  }
}
</script>

<style lang="less" scoped>
#login {
  height: 100%;
  background-image: url(@/assets/login-bg.jpg);
  background-size: cover;
  background-position: center;
}

.login-form {
  min-width: 400px;
  height: 300px;
  background-color: rgba(255, 255, 255, 0.8);
  padding: 30px;
  border-radius: 3px;
  text-align: center;
}
</style>
